import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat.js";
import quarterOfYear from "dayjs/plugin/quarterOfYear.js";
import { STATUS } from "../systems/commonConifg.js";

dayjs.extend(customParseFormat);
dayjs.extend(quarterOfYear);

/**
 * 日期转换
 * @param date
 * @param isReturnToday 如果转换失败是否返回当天日期
 */
export function formattedDate(date, isReturnToday) {
  const dateArrReg = [
    "YYYY-MM-DD HH:mm:ss",
    "YYYY/MM/DD",
    "YYYY/M/D",
    "YYYY-MM-DD",
    "YYYY-M-D",
    "YYYY.MM.DD",
    "YYYY.M.D",
  ];
  if (!Number.isNaN(Number(date)) && date !== "") {
    // 判断excel数值型日期是否能转换成日期
    return dayjs("1900-01-01")
      .add(date - 2, "day")
      .format("YYYY-MM-DD");
  } else if (dayjs(date, dateArrReg).isValid()) {
    return dayjs(date, dateArrReg).format("YYYY-MM-DD");
  }
  if (isReturnToday) {
    return dayjs().format("YYYY-MM-DD");
  }
  return "";
}

export function setPersonMessage(person, NOTE, { status = STATUS.WRONG } = {}) {
  return { ...person, NOTE, STATUS: status };
}

/**
 * 随机数生成 只有min, max且都为整数，则随机生成[min, max]的整数。
 * @param min
 * @param max
 * @param toFixed 有这个值，则为保留多少位小数。用于生成小数范围的随机数
 * @returns {string|number}
 */
export function getRandomNum(min, max, toFixed) {
  if (!toFixed) {
    return Math.floor(Math.random() * (max - min + 1) + min);
  } else {
    return (Math.random() * (max - min) + min).toFixed(toFixed);
  }
}

export function* getNameIterator({ begin = 1, end = 1, prefix = "" }) {
  while (begin <= end) {
    yield `${prefix}${begin++}`;
  }
}
